<?php

namespace app\index\controller;

use think\Request;
use app\index\model\Exchange;
use app\index\model\Variety;
use app\index\model\Contract;

class Test extends Common
{
    private $Futures;
    private $Exchange;
    private $Variety;
    private $Contract;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->Futures = new Futures();
        $this->Exchange = new Exchange();
        $this->Variety = new Variety();
        $this->Contract = new Contract();
    }

    /**
     * 获取合约列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $type_array = array('d', '1', '5', '30', '60', 'w', 'm', 'y');
        $defalt = array(
            'szCode' => '-',
            'szName' => '-',
            'szMarket' => '-',
            'lastPrice' => '-',
            'lastVol' => '-',
            'buyPrice' => '-',
            'sellPrice' => '-',
            'buyVol' => '-',
            'sellVol' => '-',
            'tradeVol' => '-',
            'lcVol' => '-',
            'lcGapVol' => '-',
            'jsPrice' => '-',
            'openPrice' => '-',
            'highPrice' => '-',
            'lowPrice' => '-',
            'prejsPrice' => '-',
            'tick' => '-',
            'upperlimit' => '-',
            'lowerlimit' => '-',
        );
        $type = input('post.type');
        $code = input('post.code');
        $exchange = input('post.exchange');
        $k_type = input('post.k_type');
        $arr['type'] = $type;
        $arr['code'] = $code;
        $arr['k_type'] = $k_type;
        $arr['exchange'] = $exchange;
        if (empty($type)) {
            $r = msg_handle('参数错误', 0);
            return json($r);
        } else {
            switch ($type) {
                case 1:
                    if (empty($arr['exchange'])) {
                        $r = msg_handle('交易所参数错误', 0);
                        return json($r);
                    } else {
                        $data = $this->Futures->index($type, $arr);
                        $list = $this->get_contract($exchange);
//                        return json($list);
                        foreach ($list as $key => $value) {
                            foreach ($data as $k => $v) {
                                if ($value['code'] == $v['szCode']) {
                                    $v['lastPrice']=round($v['lastPrice'],2);
                                    $list[$key] = $v;
                                    $list[] = $v;
                                } else {
                                    $list[$key] = $defalt;
                                    $list[$key]['szName'] = $value['name'];
                                }
                            }
                        }
                        return json($list);
                    }
                    break;
                case 2:
                    if (empty($code)) {
                        $r = msg_handle('产品代码不能为空', 0);
                        return json($r);
                    }
                    break;
                case 3:
                    if (empty($code)) {
                        $r = msg_handle('产品代码不能为空', 0);
                        return json($r);
                    } elseif (empty($k_type)) {
                        $r = msg_handle('K线类型不能为空', 0);
                        return json($r);
                    } elseif (!in_array($k_type, $type_array)) {
                        $r = msg_handle('K线类型有误', 0);
                        return json($r);
                    }
                    break;
                case 4:
                    if (empty($code)) {
                        $r = msg_handle('产品代码不能为空', 0);
                        return json($r);
                    } else {
                        $data = $this->Futures->index(1, $arr);
                        foreach ($data as $key => &$value) {
                            if ($value['szCode'] == $code) {
                                $res = $value;
                            }
                        }
                        $r = msg_handle('', 1, $res);
                        return json($r);
                    }
                    break;
            }
        }
        $data = $this->Futures->index($type, $arr);
        $r = msg_handle('', 1, $data);
        return json($r);
    }

    /**
     * 获取交易所合约信息
     * @param int $eid
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function get_contract($eid = 1)
    {
        header("Content-type: text/html; charset=utf-8");
        $exchange = $this->Exchange->where('type', $eid)->find();
        $res = array();
        if (!empty($exchange)) {
            $list = $this->Variety->where('exchange_id', $exchange['id'])->select()->toArray();
            foreach ($list as $key => &$value) {
                $arr = $this->Contract->where('variety_id', $value['id'])->select()->toArray();
                $res = array_merge($res, $arr);
            }
        }
        return $res;
    }

    /**
     * 获取所有交易所信息
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function exchange()
    {
        return $this->Exchange->select();
    }

    public function test()
    {
        $type_array = array('d', '1', '5', '30', '60', 'w', 'm', 'y');
        $defalt = array(
            'szCode' => '-',
            'szName' => '-',
            'szMarket' => '-',
            'lastPrice' => '-',
            'lastVol' => '-',
            'buyPrice' => '-',
            'sellPrice' => '-',
            'buyVol' => '-',
            'sellVol' => '-',
            'tradeVol' => '-',
            'lcVol' => '-',
            'lcGapVol' => '-',
            'jsPrice' => '-',
            'openPrice' => '-',
            'highPrice' => '-',
            'lowPrice' => '-',
            'prejsPrice' => '-',
            'tick' => '-',
            'upperlimit' => '-',
            'lowerlimit' => '-',
        );
        $type = input('get.type');
        $code = input('get.code');
        $exchange = input('get.exchange');
        $k_type = input('get.k_type');
        $arr['type'] = $type;
        $arr['code'] = $code;
        $arr['k_type'] = $k_type;
        $arr['exchange'] = $exchange;
        if (empty($type)) {
            $r = msg_handle('参数错误', 0);
            return json($r);
        } else {
            switch ($type) {
                case 1:
                    if (empty($arr['exchange'])) {
                        $r = msg_handle('交易所参数错误', 0);
                        return json($r);
                    } else {
                        $data = $this->Futures->index($type, $arr);
                        $list = $this->get_contract($exchange);
                        foreach ($list as $key => $value) {
                            foreach ($data as $k => $v) {
                                if ($value['code'] == $v['szCode']) {
                                    $v['time']=date('Y-m-d H:i:s',time());
                                    $v['lastPrice']=round($v['lastPrice'],2);
                                    $list[$key] = $v;
                                    $list[] = $v;
                                } else {
                                    $list[$key] = $defalt;
                                    $list[$key]['szCode'] = $value['code'];
                                    $list[$key]['szName'] = $value['name'];
                                    $list[$key]['time'] = date('Y-m-d H:i:s',time());
                                }
                            }
                        }
                        return json($list);
                    }
                    break;
                case 2:
                    if (empty($code)) {
                        $r = msg_handle('产品代码不能为空', 0);
                        return json($r);
                    }
                    break;
                case 3:
                    if (empty($code)) {
                        $r = msg_handle('产品代码不能为空', 0);
                        return json($r);
                    } elseif (empty($k_type)) {
                        $r = msg_handle('K线类型不能为空', 0);
                        return json($r);
                    } elseif (!in_array($k_type, $type_array)) {
                        $r = msg_handle('K线类型有误', 0);
                        return json($r);
                    }
                    break;
                case 4:
                    if (empty($code)) {
                        $r = msg_handle('产品代码不能为空', 0);
                        return json($r);
                    } else {
                        $data = $this->Futures->index(1, $arr);
                        foreach ($data as $key => &$value) {
                            if ($value['szCode'] == $code) {
                                $res = $value;
                            }
                        }
                        $r = msg_handle('', 1, $res);
                        return json($r);
                    }
                    break;
            }
        }
        $data = $this->Futures->index($type, $arr);
        $r = msg_handle('', 1, $data);
        return json($r);
    }

}